package threadLearn.future;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;

public class UseFutureWithThreadPool {

	private static ExecutorService es = Executors.newFixedThreadPool(5);

	public static void main(String[] args) throws InterruptedException, ExecutionException {
		Long start = System.currentTimeMillis();
		UseFutureWithThreadPool uf = new UseFutureWithThreadPool();
		// 创建一个任务列表
		List<FutureTask<String>> tasks = new ArrayList<>();
		for (int i = 0; i < 10; i++) {
			FutureTask<String> ft = new FutureTask<>(uf.new Dishs(i));
			tasks.add(ft);
			
		}
		
		for(FutureTask<String> ft : tasks) {
			System.out.println(ft.get());
		}
		es.shutdown();

		System.out.println("耗时：" + (System.currentTimeMillis() - start));
	}

	// 内部内
	private class Dishs implements Callable<String> {
		int count;

		public Dishs(int count) {
			this.count = count;
			System.out.format("这是第%d道菜", count);
		}

		@Override
		public String call() throws Exception {
			Thread.sleep(2000);
			return "这是菜" + count;
		}

	}
}
